package com.alisls.shequtao.service.goods.service.impl;

import com.alisls.shequtao.service.goods.dao.CategoryDAO;
import com.alisls.shequtao.service.goods.dto.CategoryDTO;
import com.alisls.shequtao.service.goods.entity.CategoryDO;
import com.alisls.shequtao.service.goods.exception.SaveCategoryException;
import com.alisls.shequtao.service.goods.service.CategoryService;
import com.alisls.shequtao.service.goods.vo.CategoryVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品分类管理
 *
 * @author Ke Wang
 */
@Service("categoryService")
@AllArgsConstructor
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryDAO, CategoryDO> implements CategoryService {

    private final CategoryDAO categoryDAO;

    /**
     * 生成树结构的 TODO
     * @return CategoryVO集合
     */
    @Override
    public List<CategoryVO> listCategoriesByTree(Long parentId) {
        CategoryVO categoryVO = CategoryVO.builder()
                                    .id(1352474866468524033L)
                                    .categoryName("家用电器")
                                    .leaf(0)
                                    .sort(1)
                                    .build();

        CategoryVO categoryVO2 = CategoryVO.builder()
                                        .id(1352962483446452225L)
                                        .categoryName("电视")
                                        .leaf(0)
                                        .sort(2)
                                        .build();

        List<CategoryVO> categoryVOChildren = new ArrayList<>();
        categoryVOChildren.add(categoryVO2);
        categoryVO.setChildren(categoryVOChildren);

        CategoryVO categoryVO3 = CategoryVO.builder()
                                        .id(1352962981188702210L)
                                        .categoryName("超薄电视")
                                        .leaf(1)
                                        .sort(3)
                                        .build();

        List<CategoryVO> categoryVO2Children = new ArrayList<>();
        categoryVO2Children.add(categoryVO3);
        categoryVO2.setChildren(categoryVO2Children);

        List<CategoryVO> categoryVOs = new ArrayList<>();
        categoryVOs.add(categoryVO);
        return categoryVOs;
    }

    @Override
    public List<CategoryDTO> listCategoriesByIds(List<Long> categoryIds) {
        List<CategoryDO> categoryDOs = categoryDAO.selectBatchIds(categoryIds);
        if (CollectionUtils.isEmpty(categoryDOs)) {
            return null;
        }
        return convertToDTOs(categoryDOs);
    }

    @Override
    public List<CategoryVO> listCategoriesByParentId(Long parentId) {
        QueryWrapper<CategoryDO> queryWrapper = new QueryWrapper();
        if (parentId != null) {
            queryWrapper.eq("parent_id", parentId);
        } else {
            queryWrapper.isNull("parent_id");
        }
        List<CategoryDO> categoryDOs = categoryDAO.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(categoryDOs)) {
            return null;
        }
        return convertToVOs(categoryDOs);
    }

    @Override
    public CategoryDTO save(CategoryDTO categoryDTO) {
        Assert.notNull(categoryDTO, "商品分类数据不能为空！");

        CategoryDO categoryDO = new CategoryDO();
        BeanUtils.copyProperties(categoryDTO, categoryDO);
        CategoryDTO parentCategoryDTO = categoryDTO.getParentCategoryDTO();
        if (parentCategoryDTO != null) {
            categoryDO.setParentId(parentCategoryDTO.getId());
        }

        if (this.save(categoryDO)) {
            categoryDTO.setId(categoryDO.getId());
            return categoryDTO;
        }

        throw SaveCategoryException.instance();
    }

    @Override
    public CategoryDTO convertToDTO(CategoryDO categoryDO) {
        if (categoryDO == null) {
            return null;
        }

        CategoryDTO categoryDTO = new CategoryDTO();
        BeanUtils.copyProperties(categoryDO, categoryDTO);

        Long parentId = categoryDO.getParentId();
        if (parentId != null) {
            CategoryDO parentCategoryDO = this.categoryDAO.selectById(parentId);
            CategoryDTO parentCategoryDTO = new CategoryDTO();
            BeanUtils.copyProperties(parentCategoryDO, parentCategoryDTO);
            categoryDTO.setParentCategoryDTO(parentCategoryDTO);
        }

        return categoryDTO;
    }

    public List<CategoryDTO> convertToDTOs(List<CategoryDO> categoryDOs) {
        if (CollectionUtils.isEmpty(categoryDOs)) {
            return null;
        }

        return categoryDOs.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    @Override
    public CategoryDO convertToDO(CategoryDTO categoryDTO) {
        CategoryDO categoryDO = new CategoryDO();
        BeanUtils.copyProperties(categoryDTO, categoryDO);
        return categoryDO;
    }

    private CategoryVO convertToVO(CategoryDO categoryDO) {
        CategoryVO categoryVO = CategoryVO.builder().build();
        BeanUtils.copyProperties(categoryDO, categoryVO);
        return categoryVO;
    }

    private List<CategoryVO> convertToVOs(List<CategoryDO> categoryDOs) {
        if (CollectionUtils.isEmpty(categoryDOs)) {
            return null;
        }

        return categoryDOs.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }
}
